package com.jd.jr.insights.dm.dim.util;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.druid.DruidPlugin;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/22.
 */
public class ModelGenerator {
    public static DataSource getDataSource() {

        //数据源配置文件路径
        String dateSourceProp = "props/datasource.properties";
        Prop prop = PropKit.use(dateSourceProp);
        String jdbcUrl = prop.get("jf_dim.mysql.jdbc.url");
        String username = prop.get("jf_dim.mysql.jdbc.username");
        String password = prop.get("jf_dim.mysql.jdbc.password");
        DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, username, password);
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }

    public static void main(String[] args) {
    start();
    }

    private  static  void start(){
        // base model 所使用的包名
        String baseModelPackageName = "com.jd.jr.insights.dm.dim.dao.model.base";
        // base model 文件保存路径
        System.out.println("PathKit.getWebRootPath()"+PathKit.getWebRootPath());
        String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/jd/jr/insights/dm/dim/dao/model/base";

        // model 所使用的包名 (MappingKit 默认使用的包名)
        String modelPackageName = "com.jd.jr.insights.dm.dim.dao.model";
        // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
        String modelOutputDir = baseModelOutputDir + "/..";

        // 创建生成器
        Generator gernerator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
        // 设置数据库方言
        gernerator.setDialect(new MysqlDialect());
        // 添加不需要生成的表名
        gernerator.addExcludedTable(getExcTab("compact_"));
        // 设置是否在 Model 中生成 dao 对象
        gernerator.setGenerateDaoInModel(true);
        // 设置是否生成字典文件
        gernerator.setGenerateDataDictionary(true);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
        gernerator.setRemovedTableNamePrefixes("t_");
        // 生成
        gernerator.generate();

    }

    private static String[] getExcTab(String preName){
        String sql="SELECT table_name from information_schema.tables WHERE table_name LIKE '"+preName+"%'";
        List<String> list = new ArrayList<String>();
        Connection conn = null;
        try {
            conn = getDataSource().getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while (rs.next()) {
                list.add(rs.getString(1));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String[] s=new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            s[i]= list.get(i);
        }
        return s;
    }
}
